package com.hl.ruoyicsmshop.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.ruoyicsmshop.domain.vo.OrderStatisticVO;
import com.hl.ruoyicsmshop.domain.vo.TransactionOverviewVO;
import com.hl.ruoyicsmshop.domain.vo.TransactionTrendVO;
import com.hl.ruoyicsmshop.domain.vo.OrderRefundDetailVO;
import com.hl.ruoyicsmshop.service.IOrderStatisticService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

/**
 * 订单统计Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/orderStatistic")
public class OrderStatisticController extends BaseController
{
    @Autowired
    private IOrderStatisticService orderStatisticService;

    /**
     * 查询热门商品订单数量统计
     */
    @RequiresPermissions("csmshop:orderStatistic:list")
    @GetMapping("/popularGoodsByQuantity")
    public AjaxResult getPopularGoodsByQuantity(
            @RequestParam(value = "dateType", defaultValue = "7") String dateType,
            @RequestParam(value = "businessId", required = false) Long businessId)
    {
        Map<String, Object> params = buildDateParams(dateType, businessId);
        List<OrderStatisticVO> list = orderStatisticService.selectPopularGoodsByQuantity(params);
        return success(list);
    }

    /**
     * 查询热门商品订单金额统计
     */
    @RequiresPermissions("csmshop:orderStatistic:list")
    @GetMapping("/popularGoodsByAmount")
    public AjaxResult getPopularGoodsByAmount(
            @RequestParam(value = "dateType", defaultValue = "7") String dateType,
            @RequestParam(value = "businessId", required = false) Long businessId)
    {
        Map<String, Object> params = buildDateParams(dateType, businessId);
        List<OrderStatisticVO> list = orderStatisticService.selectPopularGoodsByAmount(params);
        return success(list);
    }

    /**
     * 查询交易概况统计
     */
    @RequiresPermissions("csmshop:orderStatistic:list")
    @GetMapping("/transactionOverview")
    public AjaxResult getTransactionOverview(
            @RequestParam(value = "dateType", defaultValue = "7") String dateType,
            @RequestParam(value = "businessId", required = false) Long businessId)
    {
        Map<String, Object> params = buildDateParams(dateType, businessId);
        TransactionOverviewVO overview = orderStatisticService.selectTransactionOverview(params);
        return success(overview);
    }

    /**
     * 查询交易趋势统计
     */
    @RequiresPermissions("csmshop:orderStatistic:list")
    @GetMapping("/transactionTrend")
    public AjaxResult getTransactionTrend(
            @RequestParam(value = "dateType", defaultValue = "7") String dateType,
            @RequestParam(value = "businessId", required = false) Long businessId)
    {
        Map<String, Object> params = buildDateParams(dateType, businessId);
        List<TransactionTrendVO> list = orderStatisticService.selectTransactionTrend(params);
        return success(list);
    }

    /**
     * 查询订单详情列表
     */
    @RequiresPermissions("csmshop:orderStatistic:list")
    @GetMapping("/orderDetailList")
    public AjaxResult getOrderDetailList(
            @RequestParam(value = "dateType", defaultValue = "7") String dateType,
            @RequestParam(value = "businessId", required = false) Long businessId,
            @RequestParam(value = "limit", defaultValue = "20") Integer limit)
    {
        Map<String, Object> params = buildDateParams(dateType, businessId);
        params.put("limit", limit);
        List<OrderRefundDetailVO> list = orderStatisticService.selectOrderDetailList(params);
        return success(list);
    }

    /**
     * 查询退单详情列表
     */
    @RequiresPermissions("csmshop:orderStatistic:list")
    @GetMapping("/refundDetailList")
    public AjaxResult getRefundDetailList(
            @RequestParam(value = "dateType", defaultValue = "7") String dateType,
            @RequestParam(value = "businessId", required = false) Long businessId,
            @RequestParam(value = "limit", defaultValue = "20") Integer limit)
    {
        Map<String, Object> params = buildDateParams(dateType, businessId);
        params.put("limit", limit);
        List<OrderRefundDetailVO> list = orderStatisticService.selectRefundDetailList(params);
        return success(list);
    }

    /**
     * 构建日期参数
     */
    private Map<String, Object> buildDateParams(String dateType, Long businessId) {
        Map<String, Object> params = new HashMap<>();
        
        LocalDate endDate = LocalDate.now();
        LocalDate startDate;
        
        switch (dateType) {
            case "1": // 今天
                startDate = endDate;
                break;
            case "2": // 昨天
                startDate = endDate.minusDays(1);
                endDate = startDate;
                break;
            case "30": // 过去30天
                startDate = endDate.minusDays(30);
                break;
            default: // 过去7天
                startDate = endDate.minusDays(7);
                break;
        }
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        params.put("startTime", startDate.format(formatter) + " 00:00:00");
        params.put("endTime", endDate.format(formatter) + " 23:59:59");
        params.put("businessId", businessId);
        params.put("limit", 10); // 限制返回前10条记录
        
        return params;
    }
} 